#!/bin/bash

# 测试脚本 - 用于测试事务演示项目的API

BASE_URL="http://localhost:8080/api"

echo "=== 测试账户API ==="

# 获取所有账户
echo "\n获取所有账户:"
curl -s -X GET "$BASE_URL/accounts" | json_pp

# 获取特定账户
echo "\n获取账户 ACC001:"
curl -s -X GET "$BASE_URL/accounts/ACC001" | json_pp

# 存款
echo "\n向账户 ACC001 存入 500:"
curl -s -X POST "$BASE_URL/accounts/ACC001/deposit" \
  -H "Content-Type: application/json" \
  -d '{"amount": 500}' | json_pp

# 取款
echo "\n从账户 ACC002 取出 300:"
curl -s -X POST "$BASE_URL/accounts/ACC002/withdraw" \
  -H "Content-Type: application/json" \
  -d '{"amount": 300}' | json_pp

# 转账 - 成功场景
echo "\n从账户 ACC001 向账户 ACC003 转账 200:"
curl -s -X POST "$BASE_URL/accounts/transfer" \
  -H "Content-Type: application/json" \
  -d '{
    "fromAccountNumber": "ACC001",
    "toAccountNumber": "ACC003",
    "amount": 200
  }' | json_pp

# 转账 - 失败场景（余额不足）
echo "\n从账户 ACC001 向账户 ACC003 转账 10000 (应该失败):"
curl -s -X POST "$BASE_URL/accounts/transfer" \
  -H "Content-Type: application/json" \
  -d '{
    "fromAccountNumber": "ACC001",
    "toAccountNumber": "ACC003",
    "amount": 10000
  }' | json_pp

# 批量转账
echo "\n批量转账 (部分成功):"
curl -s -X POST "$BASE_URL/accounts/batch-transfer" \
  -H "Content-Type: application/json" \
  -d '[
    {
      "fromAccountNumber": "ACC002",
      "toAccountNumber": "ACC003",
      "amount": 50
    },
    {
      "fromAccountNumber": "ACC003",
      "toAccountNumber": "ACC004",
      "amount": 10000
    },
    {
      "fromAccountNumber": "ACC004",
      "toAccountNumber": "ACC001",
      "amount": 200
    }
  ]' | json_pp

# 获取所有账户（查看转账后的结果）
echo "\n获取所有账户 (转账后):"
curl -s -X GET "$BASE_URL/accounts" | json_pp

echo "\n=== 测试交易记录API ==="

# 获取所有交易记录
echo "\n获取所有交易记录:"
curl -s -X GET "$BASE_URL/transactions" | json_pp

# 获取特定账户的交易记录
echo "\n获取账户 ACC001 的交易记录:"
curl -s -X GET "$BASE_URL/transactions/account/ACC001" | json_pp

echo "\n测试完成"